package dao

import (
	"github.com/go-xorm/xorm"
	"imooc.com/demo_pms/models"
)

type PayrollDao struct {
	engine *xorm.Engine
}

func NewPayrollDao(e *xorm.Engine) *PayrollDao {
	return &PayrollDao{engine: e}
}

// 插入数据
// 参考https://www.cnblogs.com/guhao123/p/4159688.html
func (p *PayrollDao) Create(dataList []models.Payroll) (int, error) {
	i := 0
	if len(dataList) > 1 {
		i, err := p.engine.Insert(dataList)
		if err != nil {
			return int(i), err
		} else {
			return int(i), nil
		}
	}
	return i, nil
}

// 更新数据
func (p *PayrollDao) Update(data *models.Payroll,
	cols []string) error {

	_, err := p.engine.Id(data.WorkId).
		Where("time=?", data.Time).Cols(cols...).Update(data)

	return err
}
